package com.cfpl.admin_service.task;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cfpl.admin_service.VO.DueRecord;
import com.cfpl.config.RedisUtils;
import com.cfpl.admin_service.mapper.FertilizerMapper;
import com.cfpl.admin_service.mapper.PesticideTypeMapper;
import com.cfpl.admin_service.websocket.WebSocketServer;
import com.cfpl.project_common.constant.RedisConstants;
import com.cfpl.project_common.pojo.Fertilizer;
import com.cfpl.project_common.pojo.PesticideType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: LYP
 * @Date: 2023/10/25 - 10:53
 * @Description: com.cfpl.admin_service.task
 * @version: 1.0
 * 肥料信息
 * 定时任务查询
 * 预警
 */
@Component
@Slf4j
public class FertilizerTask {
    @Autowired
    private FertilizerMapper fertilizerMapper;
    @Autowired
    private PesticideTypeMapper pesticideTypeMapper;

    @Value("${vegetable.fertilizer.minimum_stock}")
    private Integer VEGETABLE_FERTILIZER_STOCK;
    @Value("${vegetable.pesticide.minimum_stock}")
    private Integer VEGETABLE_PESTICIDE_STOCK;
    @Value("${vegetable.fertilizer.remainingDays}")
    private Integer FERTILIZER_REMAINING_DAYS;
    @Value("${vegetable.pesticide.remainingDays}")
    private Integer PESTICIDE_REMAINING_DAYS;

//    @Autowired
//    private WebSocketServer webSocketServer;

    /**
     * 查询库存不足
     * 周一到周五 每隔半个小时检查一次
     * 使用WebSocket发送List
     * DayOfWeek可出现, - * / ? L # C八个字符，以及1-7的整数或SUN-SAT 单词缩写，1代表星期天，7代表星期六
     * //朝九晚五工作时间内每半小时触发一次
     *     @Scheduled(cron = "0 0/30 9-17 ? * 2,6")
     */
//    @Scheduled(cron = "0 0/30 * * * ?")
//    @Scheduled(cron = "0/30 * * * * ?")
    public void retrieveInsufficientStock(){
        Map<String, Integer> hashMap = new HashMap<>();
//查询肥料
        LambdaQueryWrapper<Fertilizer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(Fertilizer::getFertilizerStock,VEGETABLE_FERTILIZER_STOCK);
        List<Fertilizer> fertilizersStockList = fertilizerMapper.selectList(queryWrapper);
        if (!fertilizersStockList.isEmpty()){
            hashMap.put("warningStockFertilizer",fertilizersStockList.size());
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_STOCK_FERTILIZER
                                                        ,JSONUtil.toJsonStr(fertilizersStockList));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_FERTILIZER
                                                        ,String.valueOf(fertilizersStockList.size()));
        }else {
            hashMap.put("warningStockFertilizer",fertilizersStockList.size());
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_STOCK_FERTILIZER
                    ,JSONUtil.toJsonStr(fertilizersStockList));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_FERTILIZER
                    ,String.valueOf(fertilizersStockList.size()));
        }

//查询农药
        LambdaQueryWrapper<PesticideType> pesticideWrapper = new LambdaQueryWrapper<>();
        pesticideWrapper.le(PesticideType::getPesticideStock,VEGETABLE_PESTICIDE_STOCK);
        List<PesticideType> pesticideTypeList = pesticideTypeMapper.selectList(pesticideWrapper);
        if (!pesticideTypeList.isEmpty()){
            hashMap.put("warningStockPesticideType",pesticideTypeList.size());
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_STOCK_PESTICIDE
                                                        ,JSONUtil.toJsonStr(pesticideTypeList));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_PESTICIDE
                                                        ,String.valueOf(pesticideTypeList.size()));
        }else {
            hashMap.put("warningStockPesticideType",pesticideTypeList.size());
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_STOCK_PESTICIDE
                    ,JSONUtil.toJsonStr(pesticideTypeList));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_PESTICIDE
                    ,String.valueOf(pesticideTypeList.size()));
        }
//        webSocketServer.sendToAllClient(JSONUtil.toJsonStr(hashMap));
    }

    /**
     * 查询即将到期的数据
     * 绝对值小于阈值天数的施肥记录和农药记录
     */
//    @Scheduled(cron = "0/30 * * * * ?")
    public void retrieveDataOfDueSoon(){
        Map<String, Integer> hashMap = new HashMap<>();
        List<DueRecord> dueFertilizerRecord = fertilizerMapper.queryDueFertilizerRecord(FERTILIZER_REMAINING_DAYS);
        if (!dueFertilizerRecord.isEmpty()){
            //存在肥料过期记录
            hashMap.put("warningNumOfDueFertilizer", dueFertilizerRecord.size());
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_DUE_RECORD_FERTILIZER
                    ,JSONUtil.toJsonStr(dueFertilizerRecord));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_DUE_FERTILIZER
                    ,String.valueOf(dueFertilizerRecord.size()));
        }else {
//            不存在肥料过期记录  缓存空值
            hashMap.put("warningNumOfDueFertilizer", null);
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_DUE_RECORD_FERTILIZER
                    ,JSONUtil.toJsonStr(dueFertilizerRecord));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_DUE_FERTILIZER
                    ,String.valueOf(dueFertilizerRecord.size()));
        }

        List<DueRecord> duePesticideRecord = pesticideTypeMapper.queryDuePesticideRecord(PESTICIDE_REMAINING_DAYS);
        if (!duePesticideRecord.isEmpty()){
            //农药过期记录
            hashMap.put("warningNumOfDueFertilizer", duePesticideRecord.size());
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_DUE_RECORD_PESTICIDE
                    ,JSONUtil.toJsonStr(duePesticideRecord));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_DUE_PESTICIDE
                    ,String.valueOf(duePesticideRecord.size()));
        }else {
            hashMap.put("warningNumOfDueFertilizer", duePesticideRecord.size());
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_DUE_RECORD_PESTICIDE
                    ,JSONUtil.toJsonStr(duePesticideRecord));
            RedisUtils.stringRedisTemplate.opsForValue().set(RedisConstants.WARNING_NUM_DUE_PESTICIDE
                    ,String.valueOf(duePesticideRecord.size()));
        }


    }










//    @Scheduled(cron = "0/20 * * * * ?")
//    public void sendMessageToClient() {
//        log.info("——————————————————————————————");
//        Fertilizer fertilizer = fertilizerMapper.selectById(33);
//        webSocketServer.sendToAllClient("这是来自服务端的消息：" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()) + JSONUtil.toJsonStr(fertilizer));
//        webSocketServer.sendToAllClient("肥料信息：" + JSONUtil.toJsonStr(fertilizer));
//    }


}
